'''
Author: your name
Date: 2021-01-12 09:44:27
LastEditTime: 2021-01-12 10:18:10
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \Python\video.py
'''
# 功能：对视频文件进行剪切。
# 剪切指定长度的视频，选择要裁剪的视频，选择开始时间点和停止时间点即可。
# 将处理后的视频保存为output.avi文件


import cv2  # OpenCV
import tkinter.filedialog  # Python文件对话框

cap = cv2.VideoCapture("./query_video/test_video_0.mp4")

filename = tkinter.filedialog.askopenfilename()  # 弹出对话框选择需要裁剪的视频文件
cap = cv2.VideoCapture(filename)  # 打开视频文件
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)  # 获得视频文件的帧数
fps = cap.get(cv2.CAP_PROP_FPS)  # 获得视频文件的帧率
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # 获得视频文件的帧宽
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 获得视频文件的帧高

print(filename)
print(frames)
print(fps)
print(width)

# 创建保存视频文件类对象
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('11_frames.mkv', fourcc, fps, (int(width), int(height)))

# 计算视频长度/s
video_length = frames/fps
print('start and stop must < %.1f' % video_length)  # 提示用户输入变量的范围
start = float(input('Input an start time/s:'))
# stop = float(input('Input an stop time/s:'))
# 设置帧读取的开始位置
cap.set(cv2.CAP_PROP_POS_FRAMES, start*fps)
# pos = cap.get(cv2.CAP_PROP_POS_FRAMES)  # 获得帧位置
i = 0
while(i < 11):
    ret, frame = cap.read()  # 捕获一帧图像
    out.write(frame)  # 保存帧
    i += 1

print("write successfully.")
cap.release()
out.release()  # 功能：对视频文件进行剪切。
